package com.atguigu.userprofile.utils;

import com.atguigu.userprofile.beans.TagInfo;
import com.atguigu.userprofile.constant.ConstCode;
import com.google.common.base.CaseFormat;
import org.apache.commons.beanutils.BeanUtils;
import scala.collection.immutable.Stream;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 完成基于Mysql的增删改查操作
 */
public class MySQLUtil{

    public static void main(String[] args) {
        String sql = "select id , tag_code ,tag_name ,tag_level ,parent_tag_id ,tag_type,tag_value_type,tag_value_limit " +
                "tag_value_step , tag_task_id ,tag_comment ,update_time, create_time from tag_info where id = 7 " ;
        TagInfo tagInfo = selectOne(sql , TagInfo.class , true );
        System.out.println(tagInfo);
    }


    static String url = MyPropsUtil.get(ConstCode.MYSQL_URL) ;
    static String username = MyPropsUtil.get(ConstCode.MYSQL_USERNAME);
    static String password = MyPropsUtil.get(ConstCode.MYSQL_PASSWORD);
    /**
     * JDBC步骤:
     *
     * 1. 注册驱动
     * 2. 获取连接
     * 3. 编写SQL
     * 4. 预编译SQL
     * 5. 设置参数
     * 6. 执行SQL
     * 7. 处理结果集
     * 8. 关闭连接
     *
     *
     * 查询方法:
     * 1. 执行哪个SQL
     * 2. 结果封装成什么类型的对象   TagInfo  TaskInfo  TaskTagRule ....
     * 3. 是否要进行下划线到驼峰的转换
     */
    public static  <T>  T  selectOne(String sql , Class<T> cls , Boolean isUnderScoreToCamel ){
        List<T> ts = selectList(sql, cls, isUnderScoreToCamel);
        if(!ts.isEmpty()){
            return ts.get(0);
        }else{
            return null ;
        }
    }

    public static  <T> List<T> selectList(String sql , Class<T> cls , Boolean isUnderScoreToCamel  ){

        Connection connection = null ;
        PreparedStatement preparedStatement = null ;
        ResultSet resultSet = null ;

        ArrayList<T> ts = new ArrayList<>();
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取连接
            connection = DriverManager.getConnection(url, username, password);
            //3. 编写sql
            //4. 预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5. 设置参数
            //6. 执行SQL
            resultSet = preparedStatement.executeQuery();
            //7. 处理结果集
            //得到当前查询查到的列信息
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            while(resultSet.next()){
                //每条数据封装成一个T对象
                T obj = cls.newInstance();
                for (int i = 1; i <=columnCount ; i++) {
                    //获取列的名字  tag_code
                    String columnName = metaData.getColumnLabel(i);
                    //获取该列对应值
                    Object columnValue = resultSet.getObject(columnName);
                    //判断是否要进行下划线到驼峰的转换
                    String objFieldName = null ;
                    if(isUnderScoreToCamel){
                        // tag_code => tagCode
                        objFieldName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName) ;
                    }else {
                        objFieldName = columnName ;
                    }
                    //设置到对象属性上 tagCode
                    BeanUtils.copyProperty(obj , objFieldName , columnValue );
                }
                //将当前对象添加到集合中
                ts.add(obj);
            }
            //将对象返回
            return ts ;

        } catch (Exception e) {
            //e.printStackTrace();
            throw new RuntimeException("查询Mysql失败.....");
        }finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                //e.printStackTrace();
                throw new RuntimeException("关闭资源失败.....");
            }
        }
    }
}
